package com.taru.houseapp.service.impl;

import com.taru.houseapp.dao.IUserDao;
import com.taru.houseapp.entity.SearchRecord;
import com.taru.houseapp.entity.User;
import com.taru.houseapp.entity.VoPassword;
import com.taru.houseapp.service.IUserService;
import com.taru.houseapp.until.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    IUserDao userDao;

    @Override
    public User queryUserByLogin(User user) {
        User use=userDao.queryUserByLogin(user);
        User user1=null;
        if(use !=null){
            user1=new User();
            user1.setId(StringUtil.valueOf(use.getId()));
            user1.setUserName(StringUtil.valueOf(use.getUserName()));
            user1.setRealName(StringUtil.valueOf(use.getRealName()));
            user1.setIdType(StringUtil.valueOf(use.getIdType()));
            user1.setIdNumber(StringUtil.valueOf(use.getIdNumber()));
            user1.setNickName(StringUtil.valueOf(use.getNickName()));
            user1.setHeader(StringUtil.valueOf(use.getHeader()));
            user1.setTelPhone(StringUtil.valueOf(use.getTelPhone()));
            user1.setEmail(StringUtil.valueOf(use.getEmail()));
            user1.setGender(StringUtil.valueOf(use.getGender()));
            user1.setBirthday(StringUtil.valueOf(use.getBirthday()));
            user1.setStatus(StringUtil.valueOf(use.getStatus()));
            user1.setDeleted(StringUtil.valueOf(use.getDeleted()));
        }
        return user1;
    }

    @Override
    public int addUser(User user) {
        if("".equals(user.getNickName())){
            user.setNickName("L"+System.currentTimeMillis());
        }
        System.out.println(user);
        return userDao.addUser(user);
    }

    @Override
    public boolean queryUserIsExist(String userName) {
        User user=userDao.queryUserIsExist(userName);
        boolean isExist=false;
        if(user!=null){
            isExist=true;
        }
        return isExist;
    }

    @Override
    public User queryUserById(String userId) {

        return userDao.queryUserById(userId);
    }

    @Override
    public int updateUserById(User user) {

        return userDao.updateUserById(user);
    }

    @Override
    public int updatePasssword(VoPassword voPassword) {
        String password=userDao.queryPasswordById(voPassword.getUserId());
        int code=0;
        if(!password.equals(voPassword.getPassword())){
            return code;
        }
        code=userDao.updatePasswordById(voPassword);
        return code;
    }

    @Override
    public int updateHeaderById(User user) {

        return userDao.updateHeaderById(user);
    }

    @Override
    public int deleteUserById(User user) {

        return userDao.deleteUserById(user);
    }

    @Override
    public int updatePasswordByUserName(User user) {

        return userDao.updatePasswordByUserName(user);
    }

    @Override
    public List<SearchRecord> querySearchRecord(String id) {

        return userDao.querySearchRecord(id);
    }

    @Override
    public int insertSearchRecord(SearchRecord searchRecord) {
        SearchRecord record=userDao.queryRecordIsExist(searchRecord);
        if(record !=null){
            userDao.deleteOneRecord(record.getId());
        }
        return userDao.insertSearchRecord(searchRecord);
    }

    @Override
    public int deleteSearchRecord(List<Integer> ids) {

        return userDao.deleteSearchRecord(ids);
    }
}
